package cn.ulc.socket.admin.controller;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.ulc.common.model.param.PaginationParam;
import cn.ulc.common.util.Result;
import cn.ulc.socket.admin.model.dto.member.MemberListDTO;
import cn.ulc.socket.admin.model.param.member.MemberSaveParam;
import cn.ulc.socket.admin.service.member.MemberServ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/members")
public class MemberCtrl {

    private static final Log log = LogFactory.get();

    @Autowired
    MemberServ memberServ;

    @GetMapping("")
    public Result findList(String key, PaginationParam pager) {
        MemberListDTO dto = memberServ.findList(key,pager);
        return Result.success(dto);
    }
    @GetMapping("/{id}")
    public Result findDetail(@PathVariable Long id) {
        return Result.success(memberServ.findDetail(id));
    }
    @PostMapping
    public Result save(@Validated @RequestBody MemberSaveParam param, BindingResult result){
        if(result.hasErrors()) {
            log.error("保存会员详情参数错误",result.getFieldError().getDefaultMessage());
            return Result.fail(result.getFieldError().getDefaultMessage());
        }
        return Result.success(memberServ.save(param));
    }
    @PutMapping("/{id}")
    public Result update(@PathVariable("id") Long id, @RequestBody MemberSaveParam param, BindingResult result){
        if(result.hasErrors()) {
            log.error("保存会员详情参数错误",result.getFieldError().getDefaultMessage());
            return Result.fail(result.getFieldError().getDefaultMessage());
        }
        return Result.success(memberServ.update(id,param));
    }
    @DeleteMapping("/{id}")
    public Result deleted(@PathVariable("id") Long id){
        return Result.success(memberServ.deleted(id));
    }

}
